<?php
// +----------------------------------------------------------------------
// Csmmeet [ Csmonip ]
// Author: chensm <chenshiming0802@163.com>
// Create by chensm at 2020-03-31
// +----------------------------------------------------------------------
namespace addons\csmonline\library;

class Csmip
{
    private $ip2regionObj = null;

    private static $_instance = null;

    private function __construct()
    {
        $dbFile = dirname(__FILE__) . "/ip2region.db";
        $this->ip2regionObj = new Ip2Region($dbFile);
    }

    public static function getInstance()
    {
        if (! (self::$_instance instanceof Csmip)) {
            self::$_instance = new Csmip();
        }
        return self::$_instance;
    }

    /**
     * IP转成成地址
     *
     * @param
     *            $ip
     * @return \addons\csmip\library\RegionModel
     */
    public function getRegion($ip)
    {
        $vv = $this->ip2regionObj->btreeSearch($ip);
        if ($vv == null || $vv['region'] == null || $vv['region'] == "") {
            return null;
        }
        list($country, $region, $province, $city, $isp) = explode("|", $vv['region']);
        $country = ($country != "0") ? $country : "其他";
        $region = ($region != "0") ? $region : "其他";
        $province = ($province != "0") ? $province : "其他";
        $city = ($city != "0") ? $city : "其他";
        $isp = ($isp != "0") ? $isp : "其他";

        $mm = new RegionModel();
        $mm->ip = $ip;
        $mm->country = $country;
        $mm->region = $region;
        $mm->echartprovince = $this->provincetoechart($province);
        ;
        $mm->province = $province;
        $mm->city = $city;
        $mm->isp = $isp;
        $mm->ipregcityid = $vv['city_id'];
        $mm->ipregregion = $vv['region'];

        return $mm;
    }

    private function provincetoechart($province)
    {
        $sr = '';
        
        switch ($province) {
            case "北京":
                $sr = "北京";
                break;
            case "上海":
                $sr = "上海";
                break;
            case "重庆":
                $sr = "重庆";
                break;
            case "河北省":
                $sr = "河北";
                break;
            case "河南省":
                $sr = "河南";
                break;
            case "云南省":
                $sr = "云南";
                break;
            case "辽宁省":
                $sr = "辽宁";
                break;
            case "黑龙江省":
                $sr = "黑龙江";
                break;
            case "湖南省":
                $sr = "湖南";
                break;
            case "安徽省":
                $sr = "安徽";
                break;
            case "山东省":
                $sr = "山东";
                break;
            case "江苏省":
                $sr = "江苏";
                break;
            case "浙江省":
                $sr = "浙江";
                break;
            case "江西省":
                $sr = "江西";
                break;
            case "湖北省":
                $sr = "湖北";
                break;
            case "广西壮族自治区":
                $sr = "广西";
                break;
            case "甘肃省":
                $sr = "甘肃";
                break;
            case "山西省":
                $sr = "山西";
                break;
            case "内蒙古自治区":
                $sr = "内蒙古";
                break;
            case "陕西省":
                $sr = "陕西";
                break;
            case "吉林省":
                $sr = "吉林";
                break;
            case "福建省":
                $sr = "福建";
                break;
            case "贵州省":
                $sr = "贵州";
                break;
            case "广东省":
                $sr = "广东";
                break;
            case "青海省":
                $sr = "青海";
                break;
            case "西藏自治区":
                $sr = "西藏";
                break;
            case "四川省":
                $sr = "四川";
                break;
            case "宁夏回族自治区":
                $sr = "宁夏";
                break;
            case "海南省":
                $sr = "海南";
                break;
            case "台湾省":
                $sr = "台湾";
                break;
            case "香港特别行政区":
                $sr = "香港";
                break;
            case "澳门特别行政区":
                $sr = "澳门";
                break;
            case "天津市":
                $sr = "天津";
                break;
            case "新疆维吾尔自治区":
                $sr = "新疆";
                break;
            default:
                $sr = $province;
                break;
        }
        return $sr;
    }

    /**
     * 生成图统计分布画
     *
     * @param
     *            $ips
     */
    public static function drawchart($name, $ips, $needuserlogin = 'Y', $chartconfig = [])
    {
        Csmip::_defaultChartconfig($chartconfig);

        $csmip = Csmip::getInstance();
        $dao = new \app\admin\model\csmip\Data();
        $daoline = new \app\admin\model\csmip\Dataline();
        $params = [
            "name" => $name,
            "needuserlogin" => $needuserlogin,
            "chartconfig" => json_encode($chartconfig),
            "status" => 'normal',
            'createtime' => time()
        ];
        $dao->create($params);
        $dataid = $dao->getLastInsID();

        foreach ($ips as $ip) {
            $rr = $csmip->getRegion($ip);
            $params = [
                'csmip_data_id' => $dataid,
                'ip' => $ip,
                'country' => $rr->country,
                "region" => $rr->region,
                "province" => $rr->province,
                "city" => $rr->city,
                "isp" => $rr->isp,
                "ipregcityid" => $rr->ipregcityid,
                "ipregregion" => $rr->ipregregion
            ];
            $daoline->create($params);
        }
        $charturl = Csmip::_getChartUrl($dataid);
        return $charturl;
    }

    private static function _defaultChartconfig(&$chartconfig)
    {
        if (! array_key_exists('chartchina', $chartconfig)) {
            $chartconfig['chartchina'] = [];
        }
        if (! array_key_exists('pieces', $chartconfig['chartchina'])) {
            $chartconfig['chartchina']['pieces'] = [
                [
                    "gt" => 1000,
                    "label" => "> 1000 人",
                    "color" => "#7f1100"
                ],
                [
                    "gte" => 100,
                    "lte" => 1000,
                    "label" => "100 - 1000 人",
                    "color" => "#ff5428"
                ],
                [
                    "gte" => 10,
                    "lte" => 99,
                    "label" => "10 - 99 人",
                    "color" => "#ff8c71"
                ],
                [
                    "gte" => 1,
                    "lte" => 9,
                    "label" => "1 - 9 人",
                    "color" => "#ffd768"
                ],
                [
                    "value" => 0,
                    "label" => "0人",
                    "color" => "#f4f4f4"
                ]
            ];
        }
    }

    /**
     * 获取图表的地址信息
     *
     * @param
     *            $id
     * @return \addons\csmip\library\Chart
     */
    public static function _getChartUrl($id)
    {
        $baseurl = $_SERVER["REQUEST_SCHEME"] . "://" . $_SERVER["SERVER_NAME"] . config('view_replace_str.__PUBLIC__');
        $chart = new Chart();
        $chart->chartchinaurl = $baseurl . "index/csmip.chartchina/index?id=" . $id;
        return $chart;
    }
}

// 图标的地址信息
class Chart
{
    public $chartchinaurl;
}

// IP信息
class RegionModel
{
    public $ip;

    public $country;

    public $region;

    public $echartprovince;

    public $province;

    public $city;

    public $isp;

    public $ipregcityid;

    public $ipregregion;
}
